package org.vehicle.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.vehicle.dao.AlertInfoMapper;
import org.vehicle.dao.Organization_vehicleMapper;
import org.vehicle.dao.PersonInfoMapper;
import org.vehicle.dao.PositionInfoMapper;
import org.vehicle.dao.TempPersonInfoMapper;
import org.vehicle.dao.TripInfoMapper;
import org.vehicle.dao.VehicleInfoMapper;
import org.vehicle.entity.AlertInfo;
import org.vehicle.entity.OrganizationInfo;
import org.vehicle.entity.PersonInfo;
import org.vehicle.entity.TempPersonInfo;
import org.vehicle.entity.TripInfo;
import org.vehicle.entity.VehicleInfo;
import org.vehicle.service.TripService;
import org.vehicle.service.base.BaseServiceImpl;
import org.vehicle.util.JsonUtils;

@Service
public class TripServiceImpl extends BaseServiceImpl<TripInfo, Long> implements TripService{
	@Autowired
	private TripInfoMapper tripInfoMapper;
	@Autowired
	private TempPersonInfoMapper tempPersonInfoMapper;
	@Autowired
	private PositionInfoMapper positionInfoMapper;
	@Autowired
	private VehicleInfoMapper vehicleInfoMapper;
	@Autowired
	private PersonInfoMapper personInfoMapper;
	@Autowired
	private Organization_vehicleMapper organization_vehicleMapper;
	@Autowired
	private AlertInfoMapper alertInfoMapper;
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void startOrStopTrip(TripInfo tripInfo, TempPersonInfo tempPersonInfo,VehicleInfo vehicleInfo) {
		if(tripInfo.getTripState()==0){
			//修改行程记录行程位置
			TripInfo tempTripinfo=tripInfoMapper.findTripInfoByVehicleLicense(vehicleInfo.getVehicleLicense());
			tripInfo.setTripId(tempTripinfo.getTripId());
			tripInfo.setTripStopDate(new Date());
			List<Map<String, Object>> positionInfoList=positionInfoMapper.findPositionListByTripId(tripInfo.getTripId());
			tripInfo.setPositionList(JsonUtils.toJson(positionInfoList)); 
			tripInfoMapper.updateByPrimaryKeySelective(tripInfo);
			positionInfoMapper.delPositionByTripId(tripInfo.getTripId());
			//删除临时人员信息
			tempPersonInfoMapper.deleteTempPersonInfoByDeviceCode(tempPersonInfo.getDeviceCode());
			//清除车辆数据
			vehicleInfo.setVehicleType(1);
			vehicleInfo.setDeviceCheckPersonCount(0);
			vehicleInfo.setCheckPersonCount(0);
			vehicleInfo.setNotScanPersonCount(0);
			vehicleInfo.setPersonIn(0);
			vehicleInfo.setPersonOut(0);
			vehicleInfo.setPersonCount(0);
			vehicleInfoMapper.updateByPrimaryKeySelective(vehicleInfo);
			//修改告警人员
			List<PersonInfo> personInfoList=personInfoMapper.findPersonInfoByVehicleLicense(vehicleInfo.getVehicleLicense());
			List<AlertInfo> alertInfoList=alertInfoMapper.findAlertInfoByPersonInfoList(personInfoList);
			for(AlertInfo alertInfo:alertInfoList){
				alertInfo.setPersonInfoId(null);
				alertInfoMapper.updateByPrimaryKey(alertInfo);
			}
			//删除人员信息
			personInfoMapper.deletePersonInfoByVehicleInfoId(vehicleInfo.getVehicleInfoId());
			//删除车辆经过的管理站信息
			organization_vehicleMapper.deletOrganizationInfoByVehicleInfoId(vehicleInfo.getVehicleInfoId());
		}else{
			tripInfo.setTripStartDate(new Date());
			tripInfoMapper.insertSelective(tripInfo);
			Map<String, Object> paraMap =new HashMap<String, Object>();
			paraMap.put("certificateCode", tempPersonInfo.getCertificateCode());
			paraMap.put("deviceCode", tempPersonInfo.getDeviceCode());
			TempPersonInfo tempPersonInfo2=tempPersonInfoMapper.findTempPersonByCertificateCodeAndDeviceCode(paraMap);
			if(tempPersonInfo2==null){
				tempPersonInfoMapper.insertSelective(tempPersonInfo);
			}else{
				tempPersonInfoMapper.updateByPrimaryKeySelective(tempPersonInfo);
			}
		}
	}

	public TripInfo findTripInfoByVehicleLicense(TripInfo tripinfo) {
		// TODO Auto-generated method stub
			return tripInfoMapper.findTripInfoByVehicleLicense(tripinfo.getVehicleLicense());

	}

	@Override
	public Map<String, Object> gettripInfoList(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				List<OrganizationInfo> organizationInfoList = tripInfoMapper.gettripInfoList(paramMap);
		    	Integer count = tripInfoMapper.gettripInfoCount(paramMap);
		    	//分页数据
		    	Map<String, Object> dataMap = new HashMap<String, Object>();
		    	dataMap.put("totalCount", count);
		    	dataMap.put("list", organizationInfoList);
		    	dataMap.put("currentPage", paramMap.get("currentPage"));
		    	return dataMap;
	}

	@Override
	public Map<String, Object> selectByKeyId(Long key) {
		// TODO Auto-generated method stub
		return tripInfoMapper.selectByKeyId(key);
	}

	
	
}